package com.hu.controller;

import com.hu.config.comment.ResponseResult;
import com.hu.config.permission.Role;
import com.hu.config.permission.Roles;
import com.hu.dal.pojo.Info;
import com.hu.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("/info")
public class InfoController {

    @Autowired
    private InfoService infoService;

    @Roles(type = Role.SUPUSER)
    @RequestMapping(value = "listInfo", method = RequestMethod.GET)
    public ResponseResult listInfo(@RequestParam(required = false, defaultValue = "1") int page,
                                   @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return infoService.getAll(page, pageSize);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseResult getById(@PathVariable("id") int id) {
        System.out.println("获取"+id);
        return infoService.getById(id);
    }

    @RequestMapping(value = "deleteInfo/{id}", method = RequestMethod.DELETE)
    public ResponseResult deleteUser(@PathVariable("id") int id) {
        System.out.println("删除"+id);
        return infoService.delete(id);
    }

    @RequestMapping(value = "updateInfo/{id}", method = RequestMethod.PUT)
    public ResponseResult updateInfo(@Valid Info info) {
        System.out.println("修改");
        return infoService.update(info);
    }

    @RequestMapping(value = "addInfo", method = RequestMethod.POST)
    public ResponseResult addInfo(String address,long time) {
        System.out.println("=======");
        Info i=new Info();
        i.setTime(new Date(time));
        i.setAddress(address);
        return infoService.insert(i);
    }
}
